﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.ServiceLayer
{
    public static class ControlManager
    {
        public static void AddTextBox(string id, string label, string type, int maxLength, double maxValue, bool required, string regex, string cssClass, string note, string initialValue, bool isExpression, short tabIndex, System.Web.UI.Page page, Control placeHolder)
        {
            //bool hasPostBackValue = false;
            //bool hasPostBackValueRange = false;

            bool isRangeControl = false;
            MixNP.Web.Classifieds.UserControls.TextBoxControl newTextBox = (MixNP.Web.Classifieds.UserControls.TextBoxControl)page.LoadControl("~/Classifieds/UserControls/TextBoxControl.ascx");
            newTextBox.ID = id;
            
            if (!string.IsNullOrWhiteSpace(page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue(newTextBox.ID, page))))
            {
                //hasPostBackValue = true;
                initialValue = page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue(newTextBox.ID, page));
                isExpression = false;
            }

            if (!string.IsNullOrWhiteSpace(page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormRangeValue(newTextBox.ID, page))))
            {
                //hasPostBackValueRange = true;
                initialValue = page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue(newTextBox.ID, page)) + "," + page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormRangeValue(newTextBox.ID, page));
                isExpression = false;
            }

            newTextBox.Note = note;

            newTextBox.CssClass = cssClass;
            newTextBox.ControlLabel = label;
            newTextBox.MaxLength = maxLength;
            newTextBox.ClientIDMode = ClientIDMode.Static;
            newTextBox.Required = required;
            newTextBox.TabIndex = tabIndex;

            if (isExpression)
            {
                newTextBox.Text = Pes.Utility.ExpressionHelper.Eval(initialValue);
            }
            else
            {
                newTextBox.Text = initialValue;
            }

            newTextBox.RegularExpression = regex;

            if (type.ToLower().Equals("memo (long text)"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.Memo;
                newTextBox.Height = 200;
            }

            if (type.ToLower().Equals("money"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.Decimal;
            }

            if (type.ToLower().Equals("integer"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.Number;
            }

            if (type.ToLower().Equals("date"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.Date;
            }

            if (type.ToLower().Equals("integer range"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.NumberRange;
                isRangeControl = true;
            }

            if (type.ToLower().Equals("money range"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.DecimalRange;
                isRangeControl = true;
            }

            if (type.ToLower().Equals("date range"))
            {
                newTextBox.InputType = MixNP.Web.Classifieds.UserControls.TextBoxControl.InputTypeEnum.DateRange;
                isRangeControl = true;
            }

            if (isRangeControl)
            {
                if (isExpression)
                {
                    if (initialValue.Split(',').Count() == 2)
                    {
                        newTextBox.Text = Pes.Utility.ExpressionHelper.Eval(initialValue.Split(',').ElementAt(0));
                        newTextBox.Text2 = Pes.Utility.ExpressionHelper.Eval(initialValue.Split(',').ElementAt(1));
                    }
                }
                else
                {
                    if (initialValue.Split(',').Count() == 2)
                    {
                        newTextBox.Text = initialValue.Split(',').ElementAt(0);
                        newTextBox.Text2 = initialValue.Split(',').ElementAt(1);
                    }
                }
            }

            placeHolder.Controls.Add(newTextBox);
        }

        public static void AddListControl(string id, string label, string list, string initialValue, bool required, string note, string cssClass, bool isExpression, string type, short tabIndex, System.Web.UI.Page page, Control placeHolder)
        {
            //bool hasPostBackValue = false;
            MixNP.Web.Classifieds.UserControls.ListControl newTextBox = (MixNP.Web.Classifieds.UserControls.ListControl)page.LoadControl("~/Classifieds/UserControls/ListControl.ascx");
            newTextBox.ID = id;
            newTextBox.TabIndex = tabIndex;

            if (!string.IsNullOrWhiteSpace(page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormCheckBoxSelection(newTextBox.ID, page))))
            {
                //hasPostBackValue = true;
                initialValue = page.Server.HtmlDecode(MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormCheckBoxSelection(newTextBox.ID, page));
            }

            newTextBox.InitialValue = initialValue;

            newTextBox.Note = note;

            newTextBox.CssClass = cssClass;
            newTextBox.ControlLabel = label;

            newTextBox.ClientIDMode = ClientIDMode.Static;
            newTextBox.Required = required;

            if (isExpression)
            {
                newTextBox.ListItems = ToListItems(list, true);
                newTextBox.InitialValue = Pes.Utility.ExpressionHelper.Eval(initialValue);
            }
            else
            {
                newTextBox.ListItems = ToListItems(list, false);
                newTextBox.InitialValue = initialValue;
            }

            if (type.ToLower().Equals("combobox list"))
            {
                newTextBox.ListControlType = MixNP.Web.Classifieds.UserControls.ListControl.ListControlTypeEnum.DropDownList;
            }

            if (type.ToLower().Equals("checkbox list"))
            {
                newTextBox.ListControlType = MixNP.Web.Classifieds.UserControls.ListControl.ListControlTypeEnum.CheckBoxList;
            }

            if (type.ToLower().Equals("radiobutton list"))
            {
                newTextBox.ListControlType = MixNP.Web.Classifieds.UserControls.ListControl.ListControlTypeEnum.RadioButtonList;
            }

            placeHolder.Controls.Add(newTextBox);

        }

        private static ListItemCollection ToListItems(string list, bool isExpression)
        {
            ListItemCollection listItems = new ListItemCollection();

            if (string.IsNullOrWhiteSpace(list))
            {
                return listItems;
            }

            string[] items = list.Split(',');
            foreach (string item in items)
            {
                string text = string.Empty;
                string value = string.Empty;

                if (item.Split('=').Count() == 1)
                {
                    text = item;
                    value = item;

                    if (isExpression)
                    {
                        text = Pes.Utility.ExpressionHelper.Eval(item);
                        value = text;
                    }
                }

                if (item.Split('=').Count() == 2)
                {
                    text = item.Split('=')[0];
                    value = item.Split('=')[1];

                    if (isExpression)
                    {
                        text = Pes.Utility.ExpressionHelper.Eval(item.Split('=')[0]);
                    }
                }


                listItems.Add(new ListItem(text, value));
            }

            return listItems;
        }
    }
}